<?php

namespace App\Models;

use CodeIgniter\Model;



class Article_model extends Model
{
    protected $DBGroup = 'default';
    protected $table = 'article';
    protected $primaryKey = 'id';
    protected $returnType = 'array';
    protected $useTimestamps = true;
    protected $useSoftDeletes = true;
    protected $createdField  = 'created_at';
    protected $updatedField  = 'updated_at';
    protected $deletedField  = 'deleted_at';
    protected $dateFormat = 'int';

    protected $allowedFields = ['id','u_id','title','body'];

    public function __construct()
    {
        // 父类 自动使用默认配置组连接数据库
        parent::__construct();
        $this->db = \Config\Database::connect();
    }

    public function create($article_info): bool
    {
        $builder = $this->db->table($this->table);
        helper('text');
        $random_str = random_string('alnum', 5);
        $slug = url_title($article_info['u_id'].$article_info['title'].$random_str);

        return  $builder->insert([
            'u_id' => $article_info['u_id'],
            'title' => $article_info['title'],
            'slug'=>$slug,
            'body' => $article_info['body'],
        ]);
    }

    public function update_article($article_id,$article_info): bool
    {
        $data = array(
            'title' => $article_info['title'],
            'body'=>$article_info['body']
        );
        $builder = $this->db->table($this->table)->where([
            'id'=>$article_id,
            'u_id'=>$article_info['u_id']
        ]);
        return $builder->update($data);
    }

    // 拿到所有博文
    public function get_articles($slug = FALSE, $limit = FALSE, $offset = FALSE, $where = FALSE,$order = FALSE)
    {
        $builder = $this->db->table($this->table);
        if($where){
            $builder->Where($where);
        }

        if($order){
            $builder->orderBy($order,"DESC");
        }

        if ($slug === FALSE) {
            $builder->orderBy('id', 'DESC');
            if ($limit) {
                return $builder->get($limit, $offset)->getResultArray();
            }
            else
                return $builder->get()->getResultArray();
        }
        if ($limit) {
            return $builder->getWhere(['slug'=> $slug],$limit, $offset)->getResultArray();
        }
        return $builder->getWhere(['slug'=> $slug])->getResultArray();
    }

    public function search_articles($param){

        $builder = $this->db->table($this->table);
        $builder->orLike([
            "title" => $param,
            "body" => $param
        ]);
        return $builder->get();
    }

    public function get_article_by_id($article_id){
        $builder = $this->db->table($this->table);
        return $builder->where("id",$article_id)->get()->getResultArray();
    }


    public function delete_articles($article_id,$u_id){
        $builder = $this->db->table($this->table);
        $data = [
            'id'=>$article_id,
            'u_id'=>$u_id,
        ];
        return $builder->delete($data);
    }

    public function check_is_exist($article_id){
        $builder = $this->db->table($this->table);
        $builder->where([
                'id' => $article_id,
            ]
        );
        $row = $builder->get()->getRowArray();
        if (isset($row)) {
            return $row;
        }
        return false;
    }
}